package com.example.yjhero.safeapp.receiver;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import com.example.yjhero.safeapp.utils.LogUtil;
import com.example.yjhero.safeapp.R;

public class SmsLostFindReciver extends BroadcastReceiver {
    private SharedPreferences mSharedPreferences;
    private ComponentName componentName;

    @Override
    public void onReceive(Context context, Intent intent) {
        mSharedPreferences=context.getSharedPreferences("config", Activity.MODE_PRIVATE);
        boolean protecting=mSharedPreferences.getBoolean("protecting",true);
        if(protecting){
            Object[] objs= (Object[]) intent.getExtras().get("pdus");
            DevicePolicyManager dpm= (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            for(Object obj:objs){
                SmsMessage smsMessage=SmsMessage.createFromPdu((byte[])obj);
                String sender=smsMessage.getOriginatingAddress();
                if(sender.startsWith("+86")){
                    sender=sender.substring(3,sender.length());
                }
                String body=smsMessage.getMessageBody();
                String safephone=mSharedPreferences.getString("safephone",null);
                if(!TextUtils.isEmpty(safephone)&sender.equals(safephone)){
                    if("#*location*#".equals(body)){
                        LogUtil.logI("SmsLostFindReciver","oneRecive | ",""+"返回位置信息");
                        //Intent service=new Intent(context,GPSLocationService.class);
                        //context.startService(service);
                        abortBroadcast();
                    }else if("#*alarm*#".equals(body)){
                        LogUtil.logI("SmsLostFindReciver","oneRecive | ",""+"播放报警音乐");
                        MediaPlayer player=MediaPlayer.create(context, R.raw.ylzs);
                        player.start();
                        abortBroadcast();
                    }else if("#*wipedata*#".equals(body)){
                        LogUtil.logI("SmsLostFindReciver","oneRecive | ",""+"远程清除数据屏");
                        dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
                        abortBroadcast();
                    }else if("#*lockScreen*#".equals(body)){
                        LogUtil.logI("SmsLostFindReciver","oneRecive | ",""+"远程锁屏");
                        dpm.lockNow();
                        abortBroadcast();
                    }
                }



            }





        }
    }
}
